package com.xbongbong.saas.enums.business;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 生产单枚举
 *
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/14 15:37
 * @since v1.0
 */
@Getter
public enum ProductionOrderEnum {
    // 生产单模板设置界面展示字段
    // 基本信息
    BASE_INFO("other_1", "enum.productionOrder.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    SHEET_NO("serialNo", "enum.productionOrder.attrName.sheet_no", "sheetNo", FieldTypeEnum.SERIALNO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    BOM_ID("text_3", "enum.productionOrder.attrName.bom_id", "bomId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    BOM_VERSION("text_4", "enum.productionOrder.attrName.bom_version", "bomVersion", FieldTypeEnum.TEXT.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    PLAN_TIME("date_1", "enum.productionOrder.attrName.plan_time", "planTime", FieldTypeEnum.DATETIME.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    MEMO("text_9", "enum.productionOrder.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    INTO_WAREHOUSE_ID("text_5", "enum.productionOrder.attrName.into_warehouse_id", "intoWarehouseId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    PRODUCT("array_3", "enum.productionOrder.attrName.product", "finishedProduct", FieldTypeEnum.SELECT_PRODUCT.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    OUT_WAREHOUSE_ID("text_7", "enum.productionOrder.attrName.out_warehouse_id", "outWarehouseId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    MATERIEL("array_4", "enum.productionOrder.attrName.materiel", "materiel", FieldTypeEnum.SELECT_PRODUCT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    OWNER_ID("ownerId", "enum.productionOrder.attrName.owner_id", "ownerId", FieldTypeEnum.OWNERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9900, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    COUSERID("coUserId", "enum.productionOrder.attrName.couserid", "coUserId", FieldTypeEnum.COUSERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9890, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    // 系统信息
    SYSTEM_INFO("other_2", "enum.productionOrder.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    CREATOR_ID("creatorId", "enum.productionOrder.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ADD_TIME("addTime", "enum.productionOrder.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UPDATE_TIME("updateTime", "enum.productionOrder.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9850, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),

    STATUS("text_2", "enum.productionOrder.attrName.status", "status", FieldTypeEnum.COMBO.getType(), 3, 1, 1, ShowTypeEnum.LIST.getCode(), 9840, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    BOM_ID_LINK_TEXT("text_10", "enum.productionOrder.attrName.bom_id_link_text", "bomIdLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    INTO_WAREHOUSE_ID_LINK_TEXT("text_11", "enum.productionOrder.attrName.into_warehouse_id_link_text", "intoWarehouseIdLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9820, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    OUT_WAREHOUSE_ID_LINK_TEXT("text_12", "enum.productionOrder.attrName.out_warehouse_id_link_text", "outWarehouseIdLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9810, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    COMPLETE_TIME("date_2", "enum.productionOrder.attrName.complete_time", "completeTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 1, ShowTypeEnum.LIST.getCode(), 9800, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //TODO 生产成品数量  、成品已经入库数量、成品未入库数量 由于使用的时候二次计算，导致统计无法拿到相关数据，暂时关闭统计功能  2019年6月4日14:02:21
    PRODUCT_NUM("num_1", "enum.productionOrder.attrName.product_num", "productNum", FieldTypeEnum.NUM.getType(), 1, 0, 1, ShowTypeEnum.LIST.getCode(), 9790, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PRODUCT_INSTOCK_NUM("num_2", "enum.productionOrder.attrName.product_instock_num", "productInstockNum", FieldTypeEnum.NUM.getType(), 1, 0, 1, ShowTypeEnum.LIST.getCode(), 9780, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PRODUCT_UNINSTOCK_NUM("num_3", "enum.productionOrder.attrName.product_uninstock_num", "productUninstockNum", FieldTypeEnum.NUM.getType(), 1, 0, 1, ShowTypeEnum.LIST.getCode(), 9770, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    INSTOCK_ID("array_1", "enum.productionOrder.attrName.instock_id", "instockId", FieldTypeEnum.COMBO_CHECK.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9760, 0, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    OUTSTOCK_ID("array_2", "enum.productionOrder.attrName.outstock_id", "outstockId", FieldTypeEnum.COMBO_CHECK.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9750, 0, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    ALL_INCOMING("num_4", "enum.productionOrder.attrName.all_incoming", "allIncoming", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9740, 0 , 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    ALL_OUTBOUND("num_5", "enum.productionOrder.attrName.all_outbound", "allOutbound", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9730, 0 , 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    HAS_RETURNED_MATERIEL("num_6", "enum.productionOrder.attrName.has_returned_materiel", "hasReturnedMateriel", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9720, 0 , 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    INSTOCK_ID_LINK_TEXT("text_18", "enum.productionOrder.attrName.instock_id_link_text", "instockIdLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9710, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    OUTSTOCK_ID_LINK_TEXT("text_19", "enum.productionOrder.attrName.outstock_id_link_text", "outstockIdLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9700, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    CLOSE_TIME("date_3", "enum.productionOrder.attrName.close_time", "closeTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 1, ShowTypeEnum.LIST.getCode(), 9690, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    PRODUCT_NAME("text_20", "enum.productionOrder.attrName.product_name", "productName", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9680, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    PRODUCT_NO("text_21", "enum.productionOrder.attrName.product_no", "productNo", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9670, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    PRODUCT_NAME_HIDE("text_22", "enum.productionOrder.attrName.product_name_hide", "productNameHide", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9660, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    ;

    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    ProductionOrderEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                        Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 对data内的字段拼上“data.”
     *
     * @param cache 当前枚举对象
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/14 15:27
     * @since v1.0
     */
    public static String getAttrConnectData(ProductionOrderEnum cache) {
        return "data." + cache.getAttr();
    }

    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     *
     * @param cache 当前枚举对象
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/14 15:27
     * @since v1.0
     */
    public static String getEsAttr4Keyword(ProductionOrderEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    /**
     * 流程中获取隐藏字段
     * @return
     */
    public static List<ProductionOrderEnum> getDisableField() {
        List<ProductionOrderEnum> productionOrderEnums = new ArrayList<>();
        for (ProductionOrderEnum productionOrderEnum : values()) {
            if (Objects.equals(productionOrderEnum.getShowType(), ShowTypeEnum.NONE.getCode()) || Objects.equals(productionOrderEnum.getEditHide(), 1)
                    || Objects.equals(productionOrderEnum.getFieldType(), FieldTypeEnum.CREATORID.getType()) || Objects.equals(productionOrderEnum.getFieldType(), FieldTypeEnum.ADDTIME.getType())) {
                productionOrderEnums.add(productionOrderEnum);
            }
        }
        return  productionOrderEnums;
    }

    /**
     * app端详情默认标题显示字段
     *
     * @author GuoJun.HU
     * @since V1.0
     * @return List
     * @date 2019/4/8 19:21
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(SHEET_NO);
    }

    /**
     * 详情默认摘要
     *
     * @author long.rao
     * @date 2019-06-27 15:47
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(OWNER_ID, PLAN_TIME);
    }

    /**
     * 把传入的字段（类型为LINK_BUSINESS_SINGLE）的转为其隐藏字段
     * @param attr 字段名
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/4 15:31
     * @since v1.0
     * @version v1.0
     */
    public static String changeToHide4Link(String attr) {
        if (Objects.equals(attr, BOM_ID.attr)) {
            return BOM_ID_LINK_TEXT.attr;
        } else if (Objects.equals(attr, INTO_WAREHOUSE_ID.attr)) {
            return INTO_WAREHOUSE_ID_LINK_TEXT.attr;
        } else if (Objects.equals(attr, OUT_WAREHOUSE_ID.attr)) {
            return OUT_WAREHOUSE_ID_LINK_TEXT.attr;
        } else if (Objects.equals(attr, PRODUCT_NAME.attr)) {
            return PRODUCT_NAME_HIDE.attr;
        }
        return attr;
    }

    /**
     * 获取生产成品数量 、成品已经入库数量、成品未入库数量的attr
     * @return
     */
    public static List<String> getExportNumAttrList(){
        return Arrays.asList(PRODUCT_NUM.getAttr(),PRODUCT_INSTOCK_NUM.getAttr(),PRODUCT_UNINSTOCK_NUM.getAttr());
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }
}
